/**
 * Created by wangmeng on 2018/8/14.
 */
var params = new Object();
var url = path + "/common/excelImport/listByPage";
$(function () {
    //设置AjaxLoading
    setAjaxLoading();
    var grid = $("#tt");
    initGrid(grid);
});

//初始化表格和数据
function initGrid(grid) {
    var columns = [
        {
            field: "excelId",
            title: "ID",
            hidden: true,
            align: "left",
            sortable: false,
            resizable: true,
            width: 60
        },
        {
            field: "excelCode",
            title: "导入编码",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 200
        },
        {
            field: "tableName",
            title: "导入表名",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 200
        },
        {
            field: "template",
            title: "excel模板",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 200
        },
        {
            field: "sheetNo",
            title: "sheet索引",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 100
        },
        {
            field: "startRow",
            title: "数据起始行",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 100
        }
    ];
    initPageGrid(grid, params, url, columns, "#tb");
    var grid_options = $("#tt").datagrid("options");
    grid_options.onDblClickRow = function (rowIndex, rowData) {
        btnEditNode();
    }
    getPage(grid, params, url);
}

//查询按钮
function btnQuery() {
    params.excelCode = $("#excelCode").val();
    params.tableName = $("#tableName").val();
    // 重新刷新分页
    params.pageNumber = 1;
    var grid = $("#tt");
    getPage(grid, params, url);
}

//重置按钮
function btnReset() {
    $("#excelCode").val('');
    $("#tableName").val('');
    var grid = $("#tt");
    params.excelCode = $("#excelCode").val();
    params.excelName = $("#excelName").val();
    // 重新刷新分页
    params.pageNumber = 1;
    getPage(grid, params, url);
}

//增加
function btnAddNode() {
    top.layer.open({
        type: 2,
        title: '导入定义 - 新增',
        shadeClose: false,
        shade: 0.3,
        area: ['1200px', '600px'],
        content: path+'/common/excelImport/toAdd', //iframe的url
        maxmin: false,
        end: function callback() {
            btnQuery();
        }
    });
}

//修改
function btnEditNode() {
    var selectedItem = $('#tt').datagrid('getSelected');
    if(!selectedItem){
        $.messager.alert("提示", "请选择需要修改的记录！", "info");
        return false;
    }
    var excelId = selectedItem.excelId;
    top.layer.open({
        type: 2,
        title: '导入定义 - 修改',
        shadeClose: false,
        shade: 0.3,
        area: ['1200px', '600px'],
        maxmin: false,
        content: path+'/common/excelImport/toUpdate?excelId='+excelId, //iframe的url
        end: function callback() {
            btnQuery();
        }
    });
}

//删除
function btnDelNode() {
    var selectedItem = $('#tt').datagrid('getSelected');
    if(!selectedItem){
        $.messager.alert("提示", "请选择需要修改的记录！", "info");
        return false;
    }
    var param = new Object();
    param.excelId = selectedItem.excelId;
    $.messager.confirm("问题", "请确认是否要删除？", function (b) {
        if (b) {
            $.ajax({
                type: "POST",
                url: path + '/common/excelImport/delete',
                data: param,
                dataType: "json",
                async: false,
                success: function callback(data) {
                    var analyMsg = data;
                    if (analyMsg.code != "0000") {
                        $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                        });
                    } else {
                        btnQuery();
                    }
                }
            });
        }
    });
}